﻿// DarkOne 2 Lyrics Panel beta4
// Code by tedGo, based on a sample by T.P Wang

var lrclyr_path = window.GetProperty("Lyrics Path (LRC)", "$directory_path(%path%)\\%artist% - %title%.lrc");
var txtlyr_path = window.GetProperty("Lyrics Path (TXT)", "$directory_path(%path%)\\%artist% - %title%.txt");
var g_textheight = 0, g_offset = 0, g_drag_y = 0, ww = 0, wh = 0, pW = 0, pH = 0;
var ui_backcol, ui_textcol, ui_font, g_active, metadb, fileinfo;
var ui_type = window.InstanceType;
var g_need_calc = true;
var g_drag = false;
var g_timer = null;

function get_colors() {
	ui_backcol = ui_type == 0 ? window.GetColorCUI(3) : window.GetColorDUI(1);
	ui_textcol = ui_type == 0 ? window.GetColorCUI(0) : window.GetColorDUI(0);
}

get_colors();

function on_colors_changed() {
	get_colors();
	window.Repaint();
}

function get_font() {
	ui_font = ui_type == 0 ? window.GetFontCUI(0) : window.GetFontDUI(0);
}

get_font();


function on_font_changed(){
	get_font();
	window.Repaint();
}

function get_loader() {
	var a, b, c;
	a = utils.CheckComponent("foo_lyricsgrabber", true);
	b = utils.CheckComponent("foo_lyricsdb", true);
	c = a ? "Lyrics Plugin" : "get from db";
	d_command = (d_loader = a || b ? true : false) && window.GetProperty("Lyrics Search Command", c);
}

get_loader();

function opt_init() {
	a_scroll = window.GetProperty("Autoscroll", false);
	a_fader = window.GetProperty("Fading (Autoscroll only!)", false);
}

opt_init();

function pad_init() {
	hPad = window.GetProperty("Padding in px, horiz.", 0);
	if (typeof hPad != "number" || hPad < 0) hPad = 0;

	vPad = window.GetProperty("Padding in px, vert.", 0);
	if (typeof vPad != "number" || vPad < 0) vPad = 0;
}

pad_init();

function get_text() {
	metadb = fb.GetNowPlaying();
	var a, b, c, d, e;

	if (metadb) {
		fileinfo = metadb.GetFileInfo();
		a = fileinfo.MetaValue(fileinfo.MetaFind("lyrics"), 0);
		b = fileinfo.MetaValue(fileinfo.MetaFind("unsynced lyrics"), 0);
		g_nolyrics = g_active ? "- No lyrics available -\nClick to search online!" : "- No lyrics available - ";

		if (c = a ? a.replace(/\[..:.*?\]/g,"").replace(/^(\r?\n)+/g,"") : b) g_text = c;
		else {
			d = utils.ReadTextFile(fb.TitleFormat(lrclyr_path).Eval());
			e = utils.ReadTextFile(fb.TitleFormat(txtlyr_path).Eval());
			g_text = d ? d.replace(/\[..:.*?\]/g,"").replace(/^(\r?\n)+/g,"") : e ? e : g_nolyrics;
		}
	} else g_text = "LYRICS PANEL\n- Lyrics only available during playback -";
}

function calc() {
	var temp_bmp = gdi.CreateImage(1, 1);
	var temp_gr = temp_bmp.GetGraphics();

	arr = temp_gr.GdiDrawText(g_text, ui_font, ui_textcol, hPad, vPad, pW, pH, 3093).toArray();
	g_textheight = arr[3] - arr[1];
	g_offset = a_scroll && g_textheight > pH ? wh / 2 : g_textheight > pH ? vPad : (wh - g_textheight) / 2;
	g_need_calc = false;
	temp_bmp.ReleaseGraphics(temp_gr);
	temp_bmp.Dispose();
	window.Repaint();
}

function applyDelta(delta) {
	if (!a_scroll) {
		var temp = g_offset + delta;

		if (temp <= vPad && temp >= wh - (g_textheight + 20 + vPad)) {
			g_offset = temp;
			window.Repaint();
		}
	}
}

function reset() {
	g_need_calc = true;
	g_offset = 0;
	get_text();
}

function CustomMenu(x, y) {
	var _menu = window.CreatePopupMenu();
	var idx;

	_menu.AppendMenuItem(0, 1, "Refresh");
	_menu.AppendMenuItem(2048, 0, 0);
	_menu.AppendMenuItem(a_scroll ? 8 : 0, 2, "Auto-scroll");
	a_scroll && _menu.AppendMenuItem(a_fader ? 8 : 0, 3, "Fading");
	_menu.AppendMenuItem(2048, 0, 0);
	_menu.AppendMenuItem(0, 4, "Properties");
	_menu.AppendMenuItem(0, 5, "Configure");

	idx = _menu.TrackPopupMenu(x, y);

	if (idx != 0) switch (idx) {
		case 1:
			reset();
			window.Repaint();
			break;

		case 2:
			window.SetProperty("Autoscroll", a_scroll ? false : true);
			opt_init();
			on_playback_new_track(metadb);
			break;

		case 3:
			window.SetProperty("Fading (Autoscroll only!)", a_fader ? false : true);
			opt_init();
			break;

		case 4:
			window.ShowProperties();
			break;

		case 5:
			window.ShowConfigure();
	}

	_menu.Dispose();
}

function on_size() {
	ww = window.Width;
	wh = window.Height;
	pW = ww - hPad * 2;
	pH = wh - vPad * 2;
	reset();
}

function on_paint(gr) {
	var pos;

	gr.FillSolidRect(0, 0, ww, wh, ui_backcol);

	pos = a_scroll && fb.PlaybackLength > 0 && g_textheight > pH ? g_offset - g_textheight * (fb.PlaybackTime / fb.PlaybackLength) : g_offset;
	g_need_calc ? calc() : gr.GdiDrawText(g_text, ui_font, ui_textcol, hPad, pos, pW, wh - pos, 2065);
    
	if (vPad > 0) {
		gr.FillSolidRect(0, 0, ww, vPad, ui_backcol);
		gr.FillSolidRect(0, wh - vPad, wh, vPad, ui_backcol);
	}
    
	if (a_scroll && a_fader && g_textheight > pH) {
		gr.FillGradRect(0, 0, ww, wh / 3, 90, ui_backcol, 0);
		gr.FillGradRect(0, wh - wh / 3, ww, wh / 3, 90, 0, ui_backcol);
	}
}

function on_mouse_lbtn_down(x, y) {
	g_drag = true;
	g_drag_y = y;
}

function on_mouse_lbtn_up(x, y) {
	g_active && g_text == g_nolyrics && fb.RunContextCommand(d_command);
	g_drag = false;
}

function on_mouse_rbtn_up(x, y) {
	if (fb.IsPlaying) {
		CustomMenu(x, y);
		return true;
	}
}

function on_mouse_move(x, y) {
	g_active && g_text == g_nolyrics && window.SetCursor(32649);
    
	if (g_drag) {
		applyDelta(y - g_drag_y);
		g_drag_y = y;
	}
}

function on_mouse_wheel(step) {
	applyDelta(step * 20);
}

function on_playback_edited() {
	var a = g_text;
	get_text();
    
	if (g_text != a) {
		g_need_calc = true;
		g_offset = 0;
		window.Repaint();
	}
}

function on_playback_new_track(metadb) {
	if (metadb) g_active = fb.PlaybackLength <= 0 || metadb.RawPath.indexOf("cdda://" ) == 0 || metadb.RawPath.indexOf("FOO_LASTFM" ) == 0 || !d_loader ? false : true;
	reset();
	window.Repaint();

	if (a_scroll) {
		g_timer && window.KillTimer(g_timer);
		g_timer = window.CreateTimerInterval(250);
	}
}

on_playback_new_track(metadb);

function on_playback_stop(reason) {
	fileinfo && fileinfo.Dispose();

	if (g_timer) {
		window.KillTimer(g_timer);
		g_timer.Dispose();
		g_timer = null;
	}

	if (reason != 2) {
		reset();
		window.Repaint();
	}
}

function on_playback_time(time) {
	window.Repaint();
}

function on_timer(id) {
	g_timer && id == g_timer.ID && window.Repaint();
}